package com.example.springboot_demo.service.Impl;

import com.example.springboot_demo.dao.BookDao;
import com.example.springboot_demo.pojo.Book;
import com.example.springboot_demo.pojo.Cart;
import com.example.springboot_demo.pojo.Page;
import com.example.springboot_demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("bookService")
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    @Override
    public Page queryBooks(String queryName, int pageNo, int pageSize) {
        //若为空，则将其赋值为""
        if (queryName == null) queryName = "";

        //对用户恶意空格进行删除
        queryName = queryName.replace(" ", "");

        //调用方法
        Page page = bookDao.queryByBookName(pageNo, pageSize, queryName);
        return page;
    }


    @Override
    public List<Book> getAllBooks() {
        return bookDao.getAllBook();
    }

    @Override
    public void deleteBook(int id) {
        bookDao.deleteBookById(id);
    }

    @Override
    public void updateBook(Book book) {
        if (bookDao.getBookById(book.getBookId()) != null) {
            bookDao.updateBook(book);
        } else {
            bookDao.addBook(book);
        }
    }

    @Override
    public Book getBookById(int bookId) {
        return bookDao.getBookById(bookId);
    }

    @Override
    public List<Book> getAllBooksByName(String queryName) {

        return bookDao.getAllBooksByName(queryName);
    }

    @Override
    public void updateDataByCart(Cart<Book> cart) {
        bookDao.updateDataByCart(cart);
    }
}
